<template>
	<view class="container">
		<view class="list-cell b-b m-t" @click="navToPage('/pages/set/userinfo')" hover-class="cell-hover" :hover-stay-time="50">
			<text class="cell-tit">{{i18n.info_title}}</text>
			<text class="cell-more yticon icon-you"></text>
		</view>
		<view class="list-cell b-b" @click="navTo('/pages/address/address')" hover-class="cell-hover" :hover-stay-time="50">
			<text class="cell-tit">{{i18n.address_title}}</text>
			<text v-if="userInfo.city">{{userInfo.city}}</text>
			<text class="cell-more yticon icon-you"></text>
		</view>
		
		<view class="list-cell m-t">
			<text class="cell-tit">{{i18n.push_title}}</text>
			<switch :checked="isPushOn" color="#fa436a" @change="switchChange" />
		</view>
		<view class="list-cell" hover-class="cell-hover" :hover-stay-time="50">
			<text class="cell-tit">{{i18n.lang_title}}</text>
			<picker @change="bindPickerChange" :value="index" :range="lang_array" range-key="lang">
				<view class="uni-input">{{lang_array[index].lang}}</view>
			</picker>
			<text class="cell-more yticon icon-you"></text>
		</view>
		<view class="list-cell log-out-btn" @click="toLogout">
			<text class="cell-tit">{{i18n.logout_title}}</text>
		</view>
		
	</view>
</template>

<script>
	import {
		mapState,
	    mapMutations  
	} from 'vuex';
	// #ifdef APP-PLUS
		import push from "@/common/push.js"
	// #endif
	export default {
		data() {
			return {
				index: 0,
				lang_array: [{val: 'zh-CN', lang: this.$lang.set_set.lang_zh_title},{val: 'en-US', lang: this.$lang.set_set.lang_en_title}],
				lang : 'zh-CN',
				isPushOn : false,
			};
		},
		onShow() {
			uni.setNavigationBarTitle({
			    title: this.$lang.set_set.title
			});
			this.lang = uni.getStorageSync('lang') ? uni.getStorageSync('lang') : 'zh-CN';
			this.index = this.lang == 'zh-CN' ? 0 : 1;
			//更新通知
			this.updatePush();
		},
		computed: {
			...mapState(['hasLogin','userInfo']),
			i18n() {
				return this.$lang.set_set
			}
		},
		methods:{
			...mapMutations(['logout']),
			navToPage(url) {
				uni.navigateTo({
					url: url
				})
			},
			radioChange(evt){
				this.lang = evt.target.value;
				console.log(evt.target)
				uni.setStorageSync('lang',  this.lang );
			},
			bindPickerChange: function(e) {
				// console.log('picker发送选择改变，携带值为：' + e.detail.value)
				this.index = e.detail.value;
				if(this.lang_array[this.index].val != this.lang){
					this.$api.msg(this.$lang.set_set.relaunch_msg);
				}
				this.lang = this.lang_array[this.index].val;
				uni.setStorageSync('lang',  this.lang );
			},
			navTo(url){
				if(!this.hasLogin){
					url = '/pages/public/login';
				}
				uni.navigateTo({  
					url
				})  
			}, 
			//退出登录
			toLogout(){
				uni.showModal({
				    content: this.$lang.set_set.confirm_logout_title,
				    success: (e)=>{
				    	if(e.confirm){
				    		this.logout();
				    		setTimeout(()=>{
				    			uni.navigateBack();
				    		}, 200)
				    	}
				    }
				});
			},
			//switch
			switchChange(e){
				let statusTip = e.detail.value ? this.$lang.set_set.open_title: this.$lang.set_set.close_title;
				this.$api.msg(`${statusTip}`+this.$lang.set_set.sms_title);
				let tmpRmsOpen = e.target.value;
				uni.setStorageSync('rms_open',  tmpRmsOpen);
				this.$store.dispatch("upstate", {key: 'rmsOpen', val: tmpRmsOpen});
				
				if(tmpRmsOpen){
				// #ifdef APP-PLUS
					push.on();
				// #endif
				}else{
				// #ifdef APP-PLUS
				console.log('close push');
					push.off();
				// #endif
				}
			},
			updatePush(){
			// #ifdef APP-PLUS
				this.isPushOn = push.isOn();
				console.log('isPushOn', this.isPushOn );
			// #endif
			}
		}
	}
</script>

<style lang='scss'>
	page{
		background: $page-color-base;
	}
	.list-cell{
		display:flex;
		align-items:baseline;
		padding: 20upx $page-row-spacing;
		line-height:60upx;
		position:relative;
		background: #fff;
		justify-content: center;
		&.log-out-btn{
			margin-top: 40upx;
			.cell-tit{
				color: $uni-color-primary;
				text-align: center;
				margin-right: 0;
			}
		}
		&.cell-hover{
			background:#fafafa;
		}
		&.b-b:after{
			left: 30upx;
		}
		&.m-t{
			margin-top: 16upx; 
		}
		.cell-more{
			align-self: baseline;
			font-size:$font-lg;
			color:$font-color-light;
			margin-left:10upx;
		}
		.cell-tit{
			flex: 1;
			font-size: $font-base + 2upx;
			color: $font-color-dark;
			margin-right:10upx;
		}
		.cell-tip{
			font-size: $font-base;
			color: $font-color-light;
		}
		switch{
			transform: translateX(16upx) scale(.84);
		}
	}
</style>
